package cn.itcast.bos.service.system.Imp;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.system.RoleDao;
import cn.itcast.bos.dao.system.UserDao;
import cn.itcast.bos.domain.system.Role;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.UserService;

@Service
@Transactional
public class UserServiceImp implements UserService {

	@Autowired
	private UserDao userDao;
	@Autowired
	private RoleDao roleDao;

	@Override
	public User findByUsername(String username) {
		return userDao.findByUsername(username);
	}


	@Override
	public void save(User model, String[] roleIds) {
		userDao.save(model);
		if (roleIds != null) {
			for (String id : roleIds) {
				Role role = roleDao.findOne(Integer.parseInt(id));
				model.getRoles().add(role);
			}
		}

	}

	@Override
	public Page<User> findAll(Specification<User> spec, Pageable pageable) {
		 return userDao.findAll(spec, pageable);
	}


}
